package com.wdf.rule.drools;

import javax.tools.SimpleJavaFileObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author moon
 * @date 2023-02-15 20:52
 * @since 1.8
 */
public class CustomJavaFileObject extends SimpleJavaFileObject {

    /**
     * 类名称
     */
    private String className;

    /**
     * 输出的字节码流
     */
    private ByteArrayOutputStream toByteArray = new ByteArrayOutputStream();

    /**
     * Construct a SimpleJavaFileObject of the given kind and with the
     * given URI.
     *
     * @param className
     */
    public CustomJavaFileObject(String className) throws URISyntaxException {
        super(new URI(className), Kind.CLASS);
        this.className = className;
    }

    @Override
    public OutputStream openOutputStream() throws IOException {
        return toByteArray;
    }

    /**
     * 获取类名
     * @return
     */
    public String getClassName() {
        return className;
    }

    /**
     * 获取字节信息
     * @return
     */
    public byte[] getByteCode() {
        return toByteArray.toByteArray();
    }
}

